package com.example.libo.huihui.adapter;

import android.content.Context;
import android.support.v4.util.Pair;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.libo.huihui.R;
import com.example.libo.huihui.been.GuoneiListEntity;
import com.squareup.picasso.Picasso;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.example.libo.huihui.BR;

/**
 * Created by libo on 2017/1/12.
 */

public class GuoneiAdapter extends RecyclerView.Adapter<GuoneiAdapter.MyViewHolder>{
    private Context context;
    private ArrayList<GuoneiListEntity.DataBean.ListBeanX> datas;

    public GuoneiAdapter(Context context,ArrayList datas){
        this.context = context;
        this.datas = datas;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = null;
        switch (viewType){
            case 1:
                itemView = LayoutInflater.from(context).inflate(R.layout.item_allbuy,parent,false);
                break;
            case 2:
                itemView = LayoutInflater.from(context).inflate(R.layout.item_huizixun,parent,false);
                break;
            case 3:
                itemView = LayoutInflater.from(context).inflate(R.layout.hot_search,parent,false);
                break;
            default:
                itemView = new View(context);
                break;
        }
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        switch (getItemViewType(position)){
            case 1:
                GuoneiListEntity.DataBean.ListBeanX.GuoneiItemBean entity = datas.get(position).getGuoneiItem();
                TextView tvTitle = (TextView) holder.getView(R.id.tv_title);
                ImageView imageView = (ImageView) holder.getView(R.id.iv_cover);
                TextView tvFrom = (TextView) holder.getView(R.id.tv_from);
                TextView tvSold = (TextView) holder.getView(R.id.tv_selled);
                TextView tvPrice = (TextView) holder.getView(R.id.tv_price);

                tvFrom.setText(entity.getMerchant().getName());
                tvSold.setText("已售" + entity.getSalesCount() + "件");
                tvTitle.setText(entity.getTitle());
                tvPrice.setText(entity.getPrice());
                Picasso.with(context).load(entity.getImageUrl()).into(imageView);
                break;

            case 2:
                GuoneiListEntity.DataBean.ListBeanX.HuiZixunBean entity2 = datas.get(position).getHuiZixun();
                TextView tvHTitle = (TextView) holder.getView(R.id.tv_huizixun_title);
                TextView tvHContent = (TextView) holder.getView(R.id.tv_huizixun_content);
                ImageView ivCover = (ImageView) holder.getView(R.id.iv_huizixun_cover);
                TextView tvTag = (TextView) holder.getView(R.id.tv_tag);
                ImageView ivTag = (ImageView) holder.getView(R.id.iv_tag);
                tvTag.setText(entity2.getGroupName());
                tvHTitle.setText(entity2.getTitle());
                tvHContent.setText(entity2.getSummary());
                Picasso.with(context).load(entity2.getImageUrl()).into(ivCover);
                Picasso.with(context).load(entity2.getGroupImage()).into(ivTag);
                break;

            case 3:
                List list = datas.get(position).getHotSearch().getList();
                HashMap<Class<?>,Pair<Integer,Integer>> map = new HashMap();
                map.put(GuoneiListEntity.DataBean.ListBeanX.HotSearchBean.ListBean.class,Pair.create(R.layout.item_hot,BR.hotsearch));
                BindAdapter bindAdapter = new BindAdapter(map,list,context);
                RecyclerView recyclerView = (RecyclerView) holder.getView(R.id.rv_hotsearch);
                recyclerView.setLayoutManager(new GridLayoutManager(context,4));
                recyclerView.setAdapter(bindAdapter);
                break;
        }

    }

    @Override
    public int getItemViewType(int position) {
        String type = datas.get(position).getType();
        if(type.equals("GuoneiItem")){
            return 1;
        }else if(type.equals("HuiZixun")){
            return 2;
        }else if(type.equals("HotSearch")){
            return 3;
        }

        return 0;
    }

    @Override
    public int getItemCount() {
        return datas == null ? 0 : datas.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder{
        private Map<Integer,View> map;

        public MyViewHolder(View itemView) {
            super(itemView);
            map = new HashMap<>();
        }

        public View getView(int resId){
            View view = null;
            if(map.containsKey(resId)){  //如果map存过这个key-id，则直接获取，否则就要findViewbyId
                map.get(resId);
            }else{
                view = itemView.findViewById(resId);
            }
            return view;
        }
    }

}
